package com.flyoil.petromp.ui.fragment.fragment_order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_course.ItemCourseCertainAdapter;
import com.flyoil.petromp.base.BaseFragment;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.entity.entity_course_list.CourseCertainEntity;
import com.flyoil.petromp.mvp_p.ListPresenter.CourseCertainListPresenter;
import com.flyoil.petromp.mvp_v.ListUIView.BaseUIView;
import com.flyoil.petromp.ui.activity.activity_matter.OrderMessageActivity;
import com.flyoil.petromp.utils.Dialog.DialogHintUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;

/**
 * Created by songzhiiyn on 2017/10/11.
 * 流程中具体某一项列表
 */

public class CourseCertainListFragment extends BaseFragment implements BaseUIView {
    private String status = "";
    private String order_type = "";
    private ItemCourseCertainAdapter adapter;
    private CourseCertainListPresenter presenter;

    @Override
    protected int getContentViewResId() {
        return R.layout.fragment_course_certain_list;
    }

    //采用静态传值的方法，是因为fragment带参构造在打包时会报错，根据谷歌官方推荐选用该方法
    public static CourseCertainListFragment newInstance(String status, String order_type) {
        CourseCertainListFragment courseCertainListFragment = new CourseCertainListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("order_type", order_type);
        bundle.putString("status", status);
        courseCertainListFragment.setArguments(bundle);
        return courseCertainListFragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        recyclerView = $(R.id.list_fragment_course_certain_list);
        setRecyviewLayoutManager(null);
        adapter = new ItemCourseCertainAdapter(mContext);
        recyclerView.setAdapter(adapter);
        presenter = new CourseCertainListPresenter(this, order_type);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        adapter.setOnItemClickListener(onItemClickListener);
        setOnRefreshListener(true);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        initdatas();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        initdatas();
    }



    @Override
    protected void getBundleData(Bundle args) {
        super.getBundleData(args);
        if (args != null) {
            status = args.getString("status");
            order_type = args.getString("order_type");
        }
    }

    @Override
    protected void initdatas() {
        super.initdatas();
        Map<String, Object> map = new HashMap<String, Object>();
        if (status.length() == 0) {
            map.put("isDraftEQ", true);
        } else {
            map.put("transformStatusEQ", status);
            map.put("isDraftEQ", false);
        }
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
        presenter.initNetworkData(map);

    }

    /**
     * 列表的点击事件
     */
    private BaseRecyAdapter.OnItemClickListener onItemClickListener = new BaseRecyAdapter.OnItemClickListener() {
        @Override
        public void Onclick(int position, Object data) {
            if (status.length() == 0) {//当表单状态是草稿箱时弹出提示框
                show_hint_dialog();
                return;
            }
            CourseCertainEntity entity = (CourseCertainEntity) data;
            Intent intent = new Intent(mContext, OrderMessageActivity.class);
            intent.putExtra("order_type", entity.getType());
            intent.putExtra("id", entity.getId());
            startActivityForResult(intent, 1010);
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //对单据进行了操作，返回列表是刷新数据刷新数据
        if (requestCode == 1010 && resultCode == RESULT_OK) {
            onRefreshData();
        }
    }

    /**
     * 显示提示用户登录电脑端进行操作的弹框
     */
    private void show_hint_dialog() {
        DialogHintUtil.show_hint_message_diolog(mContext);
        DialogHintUtil.set_dialog_content("请登录电脑端填写表单详情");
        DialogHintUtil.set_dialog_cancle(null);
        DialogHintUtil.set_dialog_confirm("确定");
        DialogHintUtil.set_confirm_onclicklistener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogHintUtil.dismiss_dialog();
            }
        });
    }

    @Override
    public void updataToUI(List data) {
        if (isFirstLoadck) {//是否是首次加载
            if (data == null) {
                onDataError("");
                return;
            }
            if (data.size() == 0) {
                onDataNull("");
                return;
            }
            onDataSucceed();
            isFirstLoadck = false;
            pageNumber++;
        } else {
            if (pageNumber == 1) {
                if (data == null) {
                    onRefreshDataError();
                    return;
                }
                onRefreshDataSucceed();
                adapter.setDataList(data);
                recyclerView.notifyDataSetChanged();
                pageNumber++;
                return;
            } else {
                if (data == null) {
                    onLoadDataError();
                    return;
                }
                if (data.size() < pageSize) {
                    onLoadDataFinish();
                } else {
                    onLoadDataSucceed();
                }
                pageNumber++;
            }
        }
        adapter.addBottonDatas(data);
        recyclerView.notifyDataSetChanged();
    }
}
